<style scoped>
.mask{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.3;
    z-index: 9;
}
.changeOd{
    position: fixed;
    top: 15%;
    left: 50%;
    transform: translateX(-50%);
    width: 1234px;
    z-index: 99;
    background-color: white;
    overflow: auto;
}
.co_top{
    height: 40px;
    background: #f8f8f8;
    display: flex;
    justify-content: space-between;
    padding: 10px 15px;
}
.co_top p{
    font-weight: 700;
}
.co_middle{
    width: 1193px;
    /* border: 1px solid black; */
    margin: 10px auto;
}
.button_box{
    background: #f8f8f8;
    width: 100%;
    height: 60px;
    padding: 15px 0 15px 15px;
    display: flex;
}
.inp_box2{
    background: #f8f8f8;
    width: 100%;
    height: 60px;
    padding: 15px 0;
    margin: 15px 0;
    display: flex;
}
.inp_box{
  display: flex;
  margin: 15px 0 10px 0;
  position: relative;
  left: -7px;
  width: 1220px;
}
.inp_box p , .inp_box2 p{
  display: inline-block;
  width: 80px;
  text-align: right;
  padding: 0 5px 0 10px;
  padding-right: 5px;
  line-height: 30px;
}

.Change_od input:focus{
    outline: none;
    border: 1px solid #2a8ffc;
  }
  .inp{
    border: 1px solid #dbdbdb;
    border-radius: 3px;
    height: 30px;
    padding: 0 15px;
  }
  .inp2{
    width: 160px;
  }
  .inp3{
    width: 60px;
    margin-right: 10px;
  }
  .inp4{
      width: 400px;
  }
  .inp5{
    width: 121px;
    margin-right: 10px;
  }
  .inp6{
    width: 160px;
  }
  .RS_button{
      margin-left: 1070px;
  }
  .mg{
    margin-left: 32px;
  }
</style>

<style>
.Change_od .changeOd .inp_box .el-input__inner {
    height: 30px;
    width: 160px;
}
.Change_od .changeOd .inp_box .sty .el-input__inner {
    height: 30px;
    width: 90px;
}
.Change_od .changeOd .inp_box .sty2 .el-input__inner {
    height: 30px;
    width: 160px;
}
.Change_od .changeOd .inp_box .sty3 .el-input__inner {
    height: 30px;
    width: 160px;
}
.Change_od .changeOd .co_middle .inp_box2 .el-input__inner {
    height: 30px;
    width: 160px;
}
.Change_od .changeOd .el-checkbox.is-bordered{
    padding: 4px 20px 9px 8px;
    height: 30px;
    width: 32px;
}

</style>

<template>
  <div class="Change_od">
    <div class="mask"></div>
    <div class="changeOd">
      <div class="co_top">
        <p>改单(客户订货)</p>
        <i @click="closeReviSion" class="el-icon-close"></i>
      </div>

      <div class="co_middle">
        <div class="button_box">
            <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
            <el-button icon="el-icon-edit-outline" @click="modification()">改单</el-button>
        </div>

        <div class="inp_box">
          <p >配货方式</p>
          <div class="sty3">
            <el-select v-model="cooperateWay">
              <el-option
              v-for="item in cooperateWayList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              </el-option>
            </el-select>
          </div>

          <p >客户</p>
          <input class="inp inp3" type="text" v-model="customerNo" @blur="customerNoBlur" :disabled="jxAgent">
          <div class="sty">
            <el-select v-model="customerName" @change="e=>{this.customerNo=e}" :disabled="jxAgent">
              <el-option
              v-for="item in customerList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              <span style="float:left">{{item.value}}</span>
              <span style="float:right">{{item.label}}</span>
              </el-option>
            </el-select>
          </div>

          <p>货号</p>
          <input class="inp inp2" type="text" v-model="kindsNumber" disabled>
          <p>客订序号</p>
          <input class="inp inp2" type="text" v-model="sequence" disabled>
          <p>售价</p>
          <input class="inp inp2" type="text" v-model="sellingPrice" disabled>

        </div>

        <div class="inp_box">
          <p>订货日期</p>
          <!-- <input class="inp inp2" type="text" v-model="inputDate"> -->
          <el-date-picker
            class="inp2"
            v-model="inputDate"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期">
          </el-date-picker>

          <p>折数</p>
          <input class="inp inp2" type="text" v-model="discount" disabled>
          <p>实收单价</p>
          <input class="inp inp2" type="text" v-model="onePrice" disabled>
          <p>备注</p>
          <input class="inp inp2" type="text" v-model="remark">
          <p>数量</p>
          <input class="inp inp2" type="text" v-model="number">
        </div>

        <div class="inp_box2">
          <p style="width:55px">货号</p>
          <div class="sty2">
          <el-input v-model="searchKindsNumber" @keyup.enter.native="search"></el-input>
          </div>
        </div>

        <el-table
          height="250"
          :data="tableData"
          highlight-current-row
          :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}"
          :cell-style="{padding:0,height:'36px'}"
          style="width: 100%"
          @row-click="rowClick">
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            prop="sequence"
            label="客户订货序号"
            width="160">
          </el-table-column>
          <el-table-column
            prop="receiptsNumber"
            label="单号"
            width="100">
          </el-table-column>
          <el-table-column
            prop="customerNo"
            label="客户编号"
            width="80">
          </el-table-column>
          <el-table-column
            prop="kindsNumber"
            label="货号"
            width="140">
          </el-table-column>
          <el-table-column
            prop="sellingPrice"
            label="售价"
            width="80">
          </el-table-column>
          <el-table-column
            prop="discount"
            label="折数"
            width="50">
          </el-table-column>
          <el-table-column
            prop="onePrice"
            label="单价"
            width="80">
          </el-table-column>
          <el-table-column
            prop="number"
            label="数量"
            width="80">
          </el-table-column>
          <el-table-column
            prop="money"
            label="金额"
            width="80">
          </el-table-column>
          <el-table-column
            prop="remark"
            label="备注"
            width="140">
          </el-table-column>
          <el-table-column
            prop="time"
            label="订货日期"
            width="160">
          </el-table-column>
          <el-table-column
            prop="cooperateWay"
            label="配货方式"
            width="80">
          </el-table-column>
          <el-table-column
            prop="operator"
            label="操作员"
            width="120">
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>
<script>
import { select, get, update } from '../../../../network/customerOrderTotal'

import today from '../../../../components/mixins/today'

export default {
  mixins: [today],
  props: {
    sequence: {
      type: String,
      default: ''
    },
    customerList: {
      type: Array,
      default: []
    },
    flag: {
      type: Boolean,
      default: false
    }
  },
  data(){
    return{
      jxAgent: document.cookie.match(RegExp('jxAgent'+'=([^;]+)'))[1] == 1 ? true : false,
      updateData: {},

      cooperateWay: '',
      cooperateWayList: [
        {value:'配货',label:'配货'},
        {value:'订货',label:'订货'},
        {value:'订断',label:'订断'},
        {value:'追断',label:'追断'}
      ],
      customerNo: '',
      customerName: '',
      kindsNumber: '',
      sellingPrice: '',
      inputDate: '',
      discount: '',
      onePrice: '',
      remark: '',
      initialSum: 0,
      number: 0,

      searchKindsNumber: '',

      tableData: []
    }
  },
  watch: {
    flag(a,b){
      if (a) {
        if (this.sequence) {
          select({orderSequence: this.sequence}).then(res => {
            if (res.data.code == 200) {
              this.tableData = res.data.data.customerOrderVOListInfo
              this.tableData.forEach(e => {
                e.sequence = e.orderSalesSequence
                e.time = e.orderTime.slice(0,10)
              })
            }
          })
        }
      }
    }
  },
  methods:{
    clearData(){
      this.inputDate = ''
      this.customerNo = ''
      this.customerName = ''
      this.sequence = ''
      this.kindsNumber = ''
      this.discount = 10
      this.cooperateWay = ''
      this.number = ''
      this.onePrice = ''
      this.remark = ''
      this.searchKindsNumber = ''
      this.tableData = []
    },
    closeReviSion(){
      this.$emit('closeReviSion')
      this.clearData()
    },

    // 客户编号失去焦点
    customerNoBlur(){
      if (this.customerNo != '') {
        for (const i of this.customerList) {
          if (this.customerNo == i.value) {
            this.customerName = i.label
            return
          }
        }
        this.$alert('无此客户编号', '', {
          confirmButtonText: '确定'
        })
        this.customerNo = ''
        this.customerName = ''
      } else {
        this.customerName = ''
      }
    },

    // isNewBuild是0不能改数量，别的都可以改
    rowClick(row){
      // console.log(row)
      this.updateData = row
      
      this.kindsNumber = row.kindsNumber
      this.inputDate = row.orderTime
      this.customerNo = row.customerNo
      this.customerName = row.customerName
      this.discount = row.discount
      this.cooperateWay = row.cooperateWay
      this.number = row.number
      this.initialSum = row.number
      this.onePrice = row.onePrice
      this.sellingPrice = row.sellingPrice
      this.remark = row.remark
    },

    // 查询
    search(){
      if (!this.searchKindsNumber) {
        this.$alert('请输入查询条件', '', {
          confirmButtonText: '确定'
        })
        return
      }
      get({kindsNumber:this.searchKindsNumber}).then(res => {
        if (res.data.code == 200) {
          this.tableData = res.data.data
          this.tableData.forEach(e => {
            e.orderTime = e.orderTime.slice(0,10)
          })
        }
      })
    },

    
    modification(){
      if (!this.customerNo) {
        this.$alert('客户不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.inputDate) {
        this.$alert('订货日期不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!String(this.number)) {
        this.$alert('数量不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.cooperateWay) {
        this.$alert('配货方式不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }

      this.updateData.orderSequence = this.updateData.orderSalesSequence
      this.updateData.cooperateWay = this.cooperateWay
      this.updateData.customerNo = this.customerNo
      this.updateData.orderTime = this.inputDate
      this.updateData.remark = this.remark
      this.updateData.kindsNumberNumMap = {}
      this.updateData.kindsNumberNumMap[this.kindsNumber] = this.number - this.initialSum

      update(JSON.stringify(this.updateData)).then(res => {
        if (res.data.code == 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message.error('修改失败')
        }
      })
    },
  }
}
</script>